package com.gxa.modules.service.accessservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.accessmapper.ResourceMapper;
import com.gxa.modules.mapper.accessmapper.RoleMapper;
import com.gxa.modules.pojo.Resource;
import com.gxa.modules.pojo.Role;
import com.gxa.modules.pojo.RoleType;
import com.gxa.modules.service.accessservice.RoleService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public R selectRole(String roleName,String page,String size) {
        R<Object> r = new R<>();
        int statrIndex=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        int endIndex=Integer.parseInt(page)*Integer.parseInt(size);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(roleMapper.selectRole(roleName,statrIndex,endIndex));
        return r;
    }

    @Override
    public R insertRole(Role role,String resourceId) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("新增失败！");

        int count=roleMapper.insertRole(role);
        if (count>0){
            long roleId=roleMapper.selectRole(role.getRoleName(),0,1).get(0).getId();
            if (role.getRoleTypeId()==1){
                List<Resource> list=resourceMapper.selectResource("","",0,20);
                for (int i = 0; i < list.size(); i++) {
                    roleMapper.insertPermissions(roleId,String.valueOf(list.get(i).getId()));
                }
            }else {
                String[] strings=resourceId.split(",");
                for (int i = 0; i < strings.length; i++) {
                    roleMapper.insertPermissions(roleId,strings[i]);
                }
            }

            r.setCode(200);
            r.setMsg("新增成功！");
        }
        return r;
    }

    @Override
    public R deleteRole(String roleId) {
        R<Object> r = new R<>();

        if ( roleId ==null || "".equals(roleId) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        String[] strings = roleId.split(",");
        for (int i=0;i<strings.length;i++) {
            roleId=strings[i];
            if(roleMapper.deleteRole(roleId)>0){

                roleMapper.deletePermissions(roleId);
                r.setCode(200);
                r.setMsg("删除成功！");
            }
        }

        if(roleMapper.deleteRole(roleId)>0){

            roleMapper.deletePermissions(roleId);
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }

    @Override
    public R updateRole(Role role,String resourceId) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("修改失败！");

        if(role.getRoleTypeId()==1){
            if (roleMapper.updateRole(role)>0){
                roleMapper.deletePermissions(String.valueOf(role.getId()));
                List<Resource> list=resourceMapper.selectResource("","",0,20);
                for (int i = 0; i < list.size(); i++) {
                    roleMapper.insertPermissions(role.getId(),String.valueOf(list.get(i).getId()));
                }
                r.setCode(200);
                r.setMsg("修改成功！");
            }
        }else if(role.getRoleTypeId()==2){

            int count=roleMapper.updateRole(role);
            if (count>0){
                String[] strings=resourceId.split(",");
                roleMapper.deletePermissions(String.valueOf(role.getId()));
                for (int i = 0; i < strings.length; i++) {
                    roleMapper.insertPermissions(role.getId(),strings[i]);
                }
                r.setCode(200);
                r.setMsg("修改成功！");
            }

        }else {
            r.setMsg("无此角色类型！");
        }

        return r;
    }
}
